package com.example.weather_forecast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class WeatherForecastActivity extends AppCompatActivity {

    private ViewPager2 weatherViewPager;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_weather_forecast);

        // 初始化UI组件
        initViews();

        // 设置ViewPager适配器
        setupViewPager();

        // 连接TabLayout和ViewPager
        setupTabLayout();
    }

    private void initViews() {
        weatherViewPager = findViewById(R.id.weatherViewPager);
        tabLayout = findViewById(R.id.tabLayout);
    }

    private void setupViewPager() {
        // 创建适配器
        WeatherPagerAdapter adapter = new WeatherPagerAdapter(this);
        weatherViewPager.setAdapter(adapter);

        // 设置预加载页面数（可选）
        weatherViewPager.setOffscreenPageLimit(1);
    }

    private void setupTabLayout() {
        new TabLayoutMediator(tabLayout, weatherViewPager,
                (tab, position) -> {
                    switch (position) {
                        case 0:
                            tab.setText("5天预报");
                            break;
                        case 1:
                            tab.setText("7天预报");
                            break;
                    }
                }).attach();
    }
}